# -*- coding: utf-8 -*-
# @Time : 2020/10/31 10:17
# @Author : Houor
# @Site : https://my.oschina.net/houor
# @File : success_failure_method.py
# @Description: 成功失败法

from ods.functions import square_equation

h = 1.0
epsilon = 1e-6
x = -2
idx = 1

func = square_equation

f1 = func(x)
print('idx = ', idx, ', x = ', x, ', h = ', h, ', f = ', f1)
idx = idx + 1

# print(abs(h))

while abs(h) > epsilon:
    f2 = func(x + h)
    print('idx = ', idx, ', \tx = ', x, ', \th = ', h, ', \tf = ', f2)
    idx = idx + 1

    if f2 < f1:
        x = x + h
        h = 2 * h
        f1 = f2
    else:
        h = -h / 4.0

print('x = ', x)
